/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simulator;

import java.util.LinkedList;

/**
 *
 * @author Administrador
 */
class RoundRobinPrioQueueScheduler extends Scheduler {
    private LinkedList<ProcessControllBlock> queuePriority0;
    private LinkedList<ProcessControllBlock> queuePriority1;    
    private static RoundRobinPrioQueueScheduler instance;
    public static RoundRobinPrioQueueScheduler getInstance(){
        if(instance == null){
            instance = new RoundRobinPrioQueueScheduler();
            return instance;
        }else{
            return instance;
        }
    }
    private RoundRobinPrioQueueScheduler() {        
        queuePriority0 = new LinkedList<ProcessControllBlock>();
        queuePriority1 = new LinkedList<ProcessControllBlock>();
        
    }

    @Override
    protected void storeRunningJob(ProcessControllBlock job) {
        job.setPriority(job.getPriority()+1);
        if(job == emptyProcess){
            return;
        }
        if(job.getPriority()==0){
            queuePriority0.push(job);
        }
        if(job.getPriority()>=1){
            queuePriority1.push(job);
        }
    }

    @Override
    protected ProcessControllBlock getNextJob(){
        //System.out.println("SCHEDULER :: \n"+this);
        if(!queuePriority0.isEmpty()){
            return queuePriority0.pollLast();
        }
        if(!queuePriority1.isEmpty()){
            return queuePriority1.pollLast();
        }
        return emptyProcess;        
    }

    @Override
    protected void insertProcess(ProcessControllBlock process) {
        process.setPriority(0);
        queuePriority0.push(process);
    }
    public String toString(){
        String result = "queue 0:\n";
        result = result + "___________\n";
        for(int i=0;i<queuePriority0.size();i++){
            result = result +" " + queuePriority0.get(i)+"\n";
        }
        result = result + "___________\n";
        result = result + "queue 1:\n";
        result = result + "___________\n";
        for(int i=0;i<queuePriority1.size();i++){
            result = result +" " + queuePriority1.get(i)+"\n";
        }
        result = result + "___________\n";
        return result;
    }

    @Override
    protected void insertEmptyProcess(ProcessControllBlock process) {
        emptyProcess = process;
    }
}
